package com.mobileares.midp.widgets.client.menu;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-11-14
 * Time: 10:53:01
 * To change this template use File | Settings | File Templates.
 */
public class GWTMenu extends MenuBar {
    public GWTMenu() {
        this.setStylePrimaryName("myMenuBar");
    }

    public GWTMenu(boolean vertical) {
        super(vertical);
        this.setStylePrimaryName("myMenuBar");
    }

    public GWTMenu(boolean vertical, MenuBar.Resources resources) {
        super(vertical, resources);
        this.setStylePrimaryName("myMenuBar");
    }

    public GWTMenu(MenuBar.Resources resources) {
        super(resources);
        this.setStylePrimaryName("myMenuBar");
    }

    public void onBrowserEvent(Event event) {
        MenuItem item = findItem(DOM.eventGetTarget(event));
        if (item instanceof GWTMenuItem) {
            GWTMenuItem myitem = (GWTMenuItem) item;
            if (!myitem.isEnable())
                return;
        }
        switch (DOM.eventGetType(event)) {

            case Event.ONMOUSEOVER: {
                if (item != null) {
                    item.addStyleDependentName("selected");
                }
                break;
            }
            case Event.ONMOUSEOUT: {
                if (item != null) {
                    getSelectedItem().removeStyleDependentName("selected");
                }
                break;
            }

        }
        super.onBrowserEvent(event);
    }

    private MenuItem findItem(Element hItem) {
        for (MenuItem item : getItems()) {
            if (DOM.isOrHasChild(item.getElement(), hItem)) {
                return item;
            }
        }
        return null;
    }
}
